package com.example.gmapfroyotest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;
import android.os.Bundle;
import android.renderscript.Sampler.Value;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MyMap extends FragmentActivity {
	 
    // Google Map
    private GoogleMap googleMap;
 // latitude and longitude
    double latitude = 45.815433;
    double longitude = 15.872500599999966;
    DataBaseHelper mdbHelper;
    Offices offices;
    int counter;
    private ArrayList mOfficesArray;
    private HashMap<Marker, Offices> mHashMap;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mdbHelper=new DataBaseHelper(MyMap.this);
        counter=0;
        mOfficesArray=new ArrayList();
        try {
            // Loading map
            initilizeMap();
 
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }
    
    /**
     * function to load map. If map is not created it will create it for you
     * @throws IOException 
     * */ 
    private void initilizeMap() {
    	try {
    		mdbHelper.createDataBase();
		} catch (Exception e) {
			// TODO: handle exception
		}
   	mdbHelper.openDataBase();
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
 
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }     
        Cursor c=mdbHelper.query("poslovniceConv",null, null, null, null, null,null);
       // Toast.makeText(this, "cursor count -> " + String.valueOf(c.getCount()), Toast.LENGTH_LONG).show();
        
       /* c.moveToFirst();
        if(!(c.isAfterLast()))
        {	
        	do
        	{
        		Toast.makeText(MyMap.this, 
        				"Lat: " + c.getString(1) +"\n" +
        				"Lon: " + c.getString(2), 
        				Toast.LENGTH_LONG).show();        		counter++;
        	}while(c.moveToNext());
        }*/
        boolean x= c.isLast();
        int ctr=0;
        if(c!=null)
        {
        /* do
         {    
        	 c.moveToNext();
        	 Offices office=new Offices();
        	 office.setmId(Integer.parseInt(c.getString(0)));
        	 office.setmLat(Double.parseDouble(c.getString(1)));
        	 office.setmLon(c.getString(2));
        	 office.setmOff(c.getString(3));
        	 office.setmAddr(c.getString(4));
        	 office.setmZip(c.getString(5));
        	 office.setmPhone(c.getString(6));
        	 office.setwTime(c.getString(7));
  	 
        	 mOfficesArray.add(office);  
        	 counter++;
        	
         }while(c.isLast());*/
        	c.moveToFirst();
        	while(!c.isAfterLast())
            {              	          	
           	 mOfficesArray.add(new Offices(Integer.parseInt(c.getString(0)), c.getString(1), c.getString(2), c.getString(3), c.getString(4), c.getString(5), c.getString(6), c.getString(7)));  
           	 counter++;
           	 c.moveToNext();
            }      
        }
        Toast.makeText(this, " ctr -> " + String.valueOf(ctr), Toast.LENGTH_LONG).show();
        PlotMarker(mOfficesArray);
       // MarkerOptions marker=new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Map");
       // marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.beer_icon));
       // googleMap.addMarker(marker);
        CameraPosition camerapPosition=new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).tilt(30).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camerapPosition));
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        //Toast.makeText(this, " counter -> " + String.valueOf(counter), Toast.LENGTH_LONG).show();
    }
    public void PlotMarker(ArrayList<Offices> markerList)
    {
    
    	if(markerList.size()>0)
    	{
    		for(Offices of : markerList)
    		{
    				MarkerOptions marker=new MarkerOptions().position(new LatLng(Double.parseDouble(of.getmLat()),Double.parseDouble(of.getmLon()))).title(of.getmOff() + '\n'+    																												of.getwTime());
    				 googleMap.addMarker(marker);
    					
    		}	
    		 CameraPosition camerapPosition=new CameraPosition.Builder().target(new LatLng(latitude, longitude)).build();
		        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camerapPosition));
		        googleMap.getUiSettings().setMyLocationButtonEnabled(true);	
    	}
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
 
}

